/*
 * @Author: 飞越疯人院 353362604@qq.com
 * @Date: 2022-09-08 09:35:02
 * @LastEditors: 飞越疯人院 353362604@qq.com
 * @LastEditTime: 2023-01-11 09:53:36
 * @FilePath: \adminstration\src\Common\Modal\TwoModal.jsx
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */

import { ThrModal } from './ThrModal'
import { useState, useRef, useEffect, useCallback } from 'react'
import { DeleteConfirmModal, OpenExcelModal } from './Modal'
import { useEventEmitter, useBoolean, useRequest } from 'ahooks'
import { Modal, Descriptions } from 'antd'
import { PageHeader } from '@ant-design/pro-layout'
import { TreeToLevel } from './config'

import { TableModal } from './MoreModal/TableModal'
import { RolesTreeModal } from './MoreModal/TreeModal'
import { EditTableModal } from './MoreModal/EditTableModal'
import { EditTableModalTwo } from './MoreModal/EditTableModalTwo'
import { DrawMap } from './MoreModal/DrawMap'
import { FieldConfiguration } from './MoreModal/FieldConfiguration'
import { ImageList } from './MoreModal/ImageList'
import { PPPBox } from './MoreModal/PPPBox'

// 二级页
import { LabReportTemplates } from './TwoModalList/LabReportTemplates'
import { TestType } from './TwoModalList/TestType'
import { SampleEnclosure } from './TwoModalList/SampleEnclosure'
import { SampleParameter } from './TwoModalList/SampleParameter'
import { SampleReport } from './TwoModalList/SampleReport'
import { SampleAddParameter } from './TwoModalList/SampleAddParameter'
import { FileManagement } from './TwoModalList/FileManagement'
import { PushFlowConfig } from './TwoModalList/PushFlowConfig'
import { Supervise } from './TwoModalList/Supervise'

import { RolePermissions } from './TwoModalList/RolePermissions'
import { RoleWechat } from './TwoModalList/RoleWechat'

import { Organization } from './TwoModalList/Organization'
import { Project } from './TwoModalList/Project'
import { EnterpriseImage } from './TwoModalList/EnterpriseImage'
import { EnterpriseFiles } from './TwoModalList/EnterpriseFiles'
import { PersonFence } from './TwoModalList/PersonFence'
import { ProjectFormItem } from './TwoModalList/ProjectFormItem'
import { ProjectFormItemValue } from './TwoModalList/ProjectFormItemValue'
import { ProjectSetUp } from './TwoModalList/ProjectSetUp'
import { ProjectFundYearPlan } from './TwoModalList/ProjectFundYearPlan'
import { ProjectFundYearStat } from './TwoModalList/ProjectFundYearStat'
import { ProjectSchedule } from './TwoModalList/ProjectSchedule'
import { SecurityProblem } from './TwoModalList/SecurityProblem'
import { SecurityDetails } from './TwoModalList/SecurityDetails'
import { PersonGroup } from './TwoModalList/PersonGroup'
import { GantryCrane } from './TwoModalList/GantryCrane'
import { BeamProduceRecordDetail } from './TwoModalList/BeamProduceRecordDetail'
import { BeamProduceRecordPersonGroup } from './TwoModalList/BeamProduceRecordPersonGroup'
import { BeamQualityReport } from './TwoModalList/BeamQualityReport'
import { ProjectCompanyInfos } from './TwoModalList/ProjectCompanyInfos'
import { ProjectSetUpOperation } from './TwoModalList/ProjectSetUpOperation'
import { ProjectSetUpPayment } from './TwoModalList/ProjectSetUpPayment'
import { ProjectCompanyMonthReport } from './TwoModalList/ProjectCompanyMonthReport'
import { ProjectBuildList } from './TwoModalList/ProjectBuildList'
import { ProjectBuildListChild } from './TwoModalList/ProjectBuildListChild'
import { CameraGroupDetail } from './TwoModalList/CameraGroupDetail'
import { CameraTimeLapseDetail } from './TwoModalList/CameraTimeLapseDetail'
import { MeterageAttachment } from './TwoModalList/MeterageAttachment'
import { PlanStatAttachment } from './TwoModalList/PlanStatAttachment'
import { GantryCraneCameras } from './TwoModalList/GantryCraneCameras'
import { DeviceCamera } from './TwoModalList/DeviceCamera'

import { Http } from '../Api'

export const TwoModal = ({ data, Off, TowKey, PageHeaderData, Permission, DefaultData }) => {
    // !!! 二级页模板
    const SecondaryList = {
        RolesTreeModal,
        TableModal,
        EditTableModal,
        DrawMap,
        EditTableModalTwo,
        FieldConfiguration,
        ImageList,
        PPPBox,
    }
    // !!! 二级页
    const [TwoModalList] = useState({
        TestType,
        LabReportTemplates,
        SampleEnclosure,
        SampleParameter,
        SampleReport,
        SampleAddParameter,
        FileManagement,
        PushFlowConfig,
        Supervise,
        RolePermissions,
        Organization,
        Project,
        EnterpriseImage,
        EnterpriseFiles,
        RoleWechat,
        PersonFence,
        ProjectFormItem,
        ProjectFormItemValue,
        ProjectSetUp,
        ProjectFundYearPlan,
        ProjectFundYearStat,
        ProjectSchedule,
        SecurityProblem,
        SecurityDetails,
        PersonGroup,
        GantryCrane,
        BeamProduceRecordDetail,
        BeamProduceRecordPersonGroup,
        BeamQualityReport,
        ProjectCompanyInfos,
        ProjectSetUpOperation,
        ProjectSetUpPayment,
        ProjectCompanyMonthReport,
        ProjectBuildList,
        ProjectBuildListChild,
        CameraGroupDetail,
        CameraTimeLapseDetail,
        MeterageAttachment,
        PlanStatAttachment,
        GantryCraneCameras,
        DeviceCamera,
    })

    const ModalList = useCallback(
        async ({ upDataEvent, TowKey, data }) => {
            const ClientData = JSON.parse(localStorage.getItem('ClientData'))
            let dataList = await TwoModalList[TowKey]({ upDataEvent, data, ClientData, Permission })
            return dataList
        },
        [Permission, TwoModalList],
    )

    const headers = { Authorization: sessionStorage.getItem('TOKEN') }
    // Table 配置
    const actionRef = useRef()
    const [paramsData, setParamsData] = useState()
    const [Scroll] = useState({ x: 600, y: 300 })

    // 额外弹出层标题与路径
    const [ModalRecord, setModalRecord] = useState()
    // 修改参数
    const [ModifyData, setModifyData] = useState()
    // 额外参数
    const [AdditionalData, setAdditionalData] = useState()
    // 删除数据确认窗口
    const [DeleteModalState, { setTrue: DeleteModalTrue, setFalse: DeleteModalFalse }] = useBoolean(false)

    // 三级页状态
    const [ThrBoxState, { setTrue: ThrBoxTrue, setFalse: ThrBoxFalse }] = useBoolean(false)
    // 三级页配置参数
    const [ThrConfig, setThrConfig] = useState()

    // excel模块
    const [ExcelModalState, { setTrue: ExcelModalTrue, setFalse: ExcelModalFalse }] = useBoolean(false)
    const [ExcelModalUuid, setExcelModalUuid] = useState()
    const [PermissionData, setPermission] = useState()

    // 直接走接口
    const { runAsync: AuditStatusApi } = useRequest(Http, {
        manual: true,
        onSuccess: () => {
            if (actionRef.current) actionRef.current?.reload()
        },
    })

    const [PPPEvent, setPPPEvent] = useState()

    // 二级页操作列表
    const upDataEvent = useEventEmitter()
    upDataEvent.useSubscription(({ type, value, data, additional, Permission, PPPEvent }) => {
        switch (type) {
            case 'ThrModal':
                setPPPEvent(PPPEvent)
                setPermission(Permission)
                setThrConfig({ value, data })
                ThrBoxTrue()
                break
            case 'delete':
                setPPPEvent(PPPEvent)
                setAdditionalData(additional)
                setModalRecord(data)
                setModifyData(value)
                DeleteModalTrue()
                break
            case 'TestExcel':
                setExcelModalUuid({ ...data })
                ExcelModalTrue()
                break
            case 'auditStatus':
                const { api, params, path } = data
                AuditStatusApi({ url: api, data: params, path })
                break
            default:
                break
        }
    })

    // 初始化数据
    const [CurrentData, setCurrentData] = useState()
    useEffect(() => {
        const getCurrentData = async () => {
            const dataList = await ModalList({ upDataEvent, TowKey, data })
            setCurrentData(dataList)
        }
        getCurrentData()
    }, [ModalList, TowKey, data, upDataEvent])

    const RefreshEvent = useEventEmitter()

    return (
        <>
            <Modal
                className="ModalTitle TwoModal"
                open={true}
                footer={null}
                maskClosable={false}
                onCancel={Off}
                destroyOnClose
                width={`80vw`}
                title={CurrentData?.type || '123'}
                centered
            >
                <div className="PageHeader">
                    <PageHeader ghost={false}>
                        <Descriptions size="small" column={3}>
                            {PageHeaderData &&
                                PageHeaderData.map((item, index) => {
                                    if (item.hideInTable || item.title === '操作') return false
                                    if (item.valueType === 'select') {
                                        return (
                                            <Descriptions.Item key={`TwoModal${index}`} label={item.title}>
                                                {item.valueEnum[data[item.dataIndex]]}
                                            </Descriptions.Item>
                                        )
                                    } else if (item.valueType === 'treeSelect') {
                                        return (
                                            <Descriptions.Item key={`TwoModal${index}`} label={item.title}>
                                                {
                                                    TreeToLevel(item.fieldProps.options).find(
                                                        v => v.uuid === data[item.dataIndex],
                                                    )?.tableName
                                                }
                                            </Descriptions.Item>
                                        )
                                    } else {
                                        return (
                                            <Descriptions.Item key={`TwoModal${index}`} label={item.title}>
                                                {data[item.dataIndex]}
                                            </Descriptions.Item>
                                        )
                                    }
                                })}
                        </Descriptions>
                    </PageHeader>
                </div>
                {CurrentData &&
                    (() => {
                        const Template = SecondaryList[CurrentData.classification]
                        return (
                            <Template
                                headers={headers}
                                actionRef={actionRef}
                                CurrentData={CurrentData}
                                data={data}
                                paramsData={paramsData}
                                setParamsData={setParamsData}
                                Scroll={Scroll}
                                RefreshEvent={RefreshEvent}
                                DefaultData={DefaultData}
                                upDataEvent={upDataEvent}
                            />
                        )
                    })()}
            </Modal>

            {/* 三级页 */}
            {ThrBoxState && (
                <ThrModal
                    Permission={PermissionData}
                    ThrConfig={ThrConfig}
                    actionRef={actionRef}
                    Off={ThrBoxFalse}
                    RefreshEvent={RefreshEvent}
                    PPPEvent={PPPEvent}
                />
            )}
            {/* 删除弹出层 */}
            {DeleteModalState && (
                <DeleteConfirmModal
                    Off={DeleteModalFalse}
                    actionRef={actionRef}
                    ModalRecord={ModalRecord}
                    ModifyData={ModifyData}
                    AdditionalData={AdditionalData}
                    PPPEvent={PPPEvent}
                />
            )}
            {/* excel弹出层 */}
            {ExcelModalState && <OpenExcelModal Off={ExcelModalFalse} data={ExcelModalUuid} />}
        </>
    )
}
